import java.awt.*;

// 游戏元素基类
abstract class GameElement {
    float x, y, duration, age;
    int size;
    Color color;
    float effectValue;

    protected float floatSpeed = 0.5f + (float) Math.random() * 0.5f; // 漂浮速度
    protected float floatDirection = (float) (Math.random() * Math.PI * 2); // 初始方向
    protected float floatChangeTimer = 0; // 方向变化计时器

    public GameElement(float x, float y, int size, Color color, float effect, float duration) {
        this.x = x;
        this.y = y;
        this.size = size;
        this.color = color;
        this.effectValue = effect;
        this.duration = duration;
    }

    public boolean update() {
        age += 1 / 60f;

        // 漂浮移动逻辑
        floatChangeTimer -= 1 / 60f;
        if (floatChangeTimer <= 0) {
            // 随机改变漂浮方向
            floatDirection += (float) ((Math.random() - 0.5) * Math.PI / 4);
            floatChangeTimer = 2 + (float) Math.random() * 3; // 2-5秒后再次改变
        }

        // 根据当前方向移动
        x += (float) (Math.cos(floatDirection) * floatSpeed);
        y += (float) (Math.sin(floatDirection) * floatSpeed);

        // 边界检查
        if (x < (float) size / 2) x = (float) size / 2;
        if (x > Main.WIDTH - (float) size / 2) x = Main.WIDTH - (float) size / 2;
        if (y < (float) size / 2) y = (float) size / 2;
        if (y > Main.HEIGHT - (float) size / 2) y = Main.HEIGHT - (float) size / 2;

        return age < duration;
    }

    public void draw(Graphics2D g) {
        float alpha = 1 - age / duration;
        g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), (int) (alpha * 200 + 55)));
        g.fillOval((int) x - size / 2, (int) y - size / 2, size, size);
    }

}
